using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MicroMsg.UI.UserContrl
{
	public class NickNameButton : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		private const string TAG = "NickNameButton";
		private const int cNMaxCacheCnt = 10;
		public NickNameButtonMode mMode;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(string), typeof(NickNameButton), new PropertyMetadata(null, new PropertyChangedCallback(NickNameButton.OnItemObjectPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		private static SolidColorBrush sBrushFocus;
		private static List<NickNameButton> sItems;
		internal EmojiText txtNickName;
		internal TextBlock txtTag;
		private bool _contentLoaded;
		public event NickNameButton.ListItemClick ItemClick;
		public string ItemObject
		{
			get
			{
				return (string)base.GetValue(NickNameButton.ItemObjectProperty);
			}
			set
			{
				base.SetValue(NickNameButton.ItemObjectProperty, value);
			}
		}
		public NickNameButton()
		{
			ShowObjectInMemory.AddOneObjectRecord(this);
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		~NickNameButton()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == NickNameButton.ItemObjectProperty)
			{
				(dependencyObject_0 as NickNameButton).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.RefreshItem();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		private void Button_Click(object sender, object object_0)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, object_0);
			}
			Contact contact = StorageMgr.contact.method_0(this.ItemObject);
			if (contact == null)
			{
				contact = new Contact();
				contact.strUsrName = this.ItemObject;
			}
			object[] passData = new object[]
			{
				contact
			};
			SnsUserMainPage.GoToThisPage(passData);
		}
		private void txtNickName_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
		{
			this.txtNickName.set_Background(NickNameButton.sBrushFocus);
		}
		private void txtNickName_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
		{
			this.txtNickName.set_Background(null);
		}
		public void SetMode(NickNameButtonMode mode)
		{
			if (mode == this.mMode)
			{
				return;
			}
			if (mode == NickNameButtonMode.WithTagComma)
			{
				this.txtTag.set_Visibility(0);
			}
			else
			{
				this.txtTag.set_Visibility(1);
			}
			if (mode == NickNameButtonMode.WithColon)
			{
				this.txtNickName.Text = this.txtNickName.Text + ":";
			}
			else
			{
				if (this.mMode == NickNameButtonMode.WithColon && this.txtNickName.Text.EndsWith(":"))
				{
					this.txtNickName.Text = this.txtNickName.Text.Substring(0, this.txtNickName.Text.get_Length() - 1);
				}
			}
			this.mMode = mode;
		}
		private void RefreshItem()
		{
			string text = ContactHelper.getDisplayName(this.ItemObject, null);
			if (this.mMode == NickNameButtonMode.WithColon)
			{
				text += ":";
			}
			this.txtNickName.Text = text;
			if (this.mMode == NickNameButtonMode.WithTagComma)
			{
				this.txtTag.set_Visibility(0);
				return;
			}
			this.txtTag.set_Visibility(1);
		}
		public static NickNameButton GetOneItem()
		{
			if (NickNameButton.sItems != null && NickNameButton.sItems.get_Count() != 0)
			{
				NickNameButton nickNameButton = Enumerable.First<NickNameButton>(NickNameButton.sItems);
				NickNameButton.clearItemParent(nickNameButton);
				NickNameButton.sItems.RemoveAt(0);
				return nickNameButton;
			}
			NickNameButton nickNameButton2 = new NickNameButton();
			nickNameButton2.set_FontSize(20.0);
			nickNameButton2.txtTag.set_FontSize(20.0);
			nickNameButton2.txtNickName.FontSize = 20.0;
			return nickNameButton2;
		}
		public static int GetCacheCnt()
		{
			if (NickNameButton.sItems == null)
			{
				return 0;
			}
			return NickNameButton.sItems.get_Count();
		}
		public static void PushOneItem(NickNameButton item)
		{
			if (item == null)
			{
				return;
			}
			item.set_Visibility(0);
			if (NickNameButton.sItems == null)
			{
				NickNameButton.sItems = new List<NickNameButton>();
			}
			if (NickNameButton.sItems.get_Count() > 10)
			{
				return;
			}
			NickNameButton.sItems.Add(item);
		}
		private static void clearItemParent(NickNameButton item)
		{
			if (item.get_Parent() != null)
			{
				Panel panel = item.get_Parent() as Panel;
				if (panel != null)
				{
					panel.get_Children().Remove(item);
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Circle/UI/NickNameButton.xaml", 2));
			this.txtNickName = (EmojiText)base.FindName("txtNickName");
			this.txtTag = (TextBlock)base.FindName("txtTag");
		}
		static NickNameButton()
		{
			// Note: this type is marked as 'beforefieldinit'.
			SolidColorBrush solidColorBrush = new SolidColorBrush();
			solidColorBrush.set_Color(Color.FromArgb(255, 153, 153, 153));
			NickNameButton.sBrushFocus = solidColorBrush;
			NickNameButton.sItems = new List<NickNameButton>();
		}
	}
}
